using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;

namespace Presentation
{
    public partial class UserControlBaocaohangtonkho : UserControl
    {
        public UserControlBaocaohangtonkho()
        {
            InitializeComponent();
        }

        private void UserControlBaocaohangtonkho_Load(object sender, EventArgs e)
        {
            txtNhanvien.Text = FrmDangNhap.ten;
        }

        private void btnTracuu_Click(object sender, EventArgs e)
        {
            //HoaDonMuaDao hdmuadao = new HoaDonMuaDao();
            //HoadonmuaDto[] hdmuadto = hdmuadao.SelectAll();
            DateTime tungay = DateTime.Parse(dtpTungay.Text);
            DateTime denngay = DateTime.Parse(dtpDenngay.Text);
            List<DuocPhamDto> list = new List<DuocPhamDto>();
            //for (int i = 0; i < hdmuadto.Length; i++)
            //{
            //    if(tungay<=hdmuadto[i].NgayTao && denngay>=hdmuadto[i].NgayTao)
            //    {
                    DuocPhamDao dpdao = new DuocPhamDao();
                    DuocPhamDto[] dpdto = dpdao.ShowCurrentItems(tungay, denngay, 0);
         
                    list.AddRange(dpdto);
                    //list.Add(hdmuadto[i]);
            //    }
            //}
            //kiem tra xem co san pham nao ton tai ko? neu co xuat thong bao ko co.
            if(list.Count==0)
            {
                MessageBox.Show(this, "không có sản phẩm hàng tồn kho.");
                return;
            }
            dataGridView_baocaohangtonkho.AutoGenerateColumns = false;
            dataGridView_baocaohangtonkho.DataSource = list.ToArray();
            float sum = 0;
            for (int i = 0; i < dataGridView_baocaohangtonkho.Rows.Count; i++)
            {
                sum = sum + float.Parse(dataGridView_baocaohangtonkho.Rows[i].Cells[8].Value.ToString());
            }
            txtTongtien.Text = Convert.ToDecimal(sum).ToString();

            btnExport.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void dataGridView_baocaohangtonkho_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView grv = (DataGridView) sender;
            Utils.MakeSothutuColumn(grv, "itemStt"); 
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            Utils.ExportToExcel(dataGridView_baocaohangtonkho);
        }
    }
}
